/**
 * 鼎亚汽车 - MapUtils
 * http://menger.me
 * @大梦
 */

import { Linking, Platform } from 'react-native';
var MyUtil = {

	/**
	 * 跳转到导航界面
	 * @param lon
	 * @param lat
	 * @param name
	 * @param targetAppName browser-浏览器打开， gaode-高德APP， baidu-百度APP，如果没有安装相应APP则使用浏览器打开。
	 */
	turn2MapApp(lon = 0, lat = 0, targetAppName = 'baidu', name = '目标地址'){
		if (0 == lat && 0 == lon) {
			console.warn('暂时不能导航');
			return;
		}

		let url = '';
		let webUrl = `http://uri.amap.com/navigation?to=${lon},${lat},${name}&mode=car&coordinate=gaode`;
		let webUrlGaode = `http://uri.amap.com/navigation?to=${lon},${lat},${name}&mode=car&coordinate=gaode`;
		let webUrlBaidu = `http://api.map.baidu.com/direction?destination=latlng:${lat},${lon}|name=${name}&mode=driving&coord_type=gcj02&output=html&src=mybaoxiu|wxy`;

		url = webUrl;
		if (Platform.OS == 'android') {//android

			if (targetAppName == 'gaode') {
				// webUrl = 'androidamap://navi?sourceApplication=appname&poiname=fangheng&lat=36.547901&lon=104.258354&dev=1&style=2';
				url = `androidamap://route?sourceApplication=appname&dev=0&m=0&t=0&dlon=${lon}&dlat=${lat}&dname=${name}`;
				webUrl = webUrlGaode;
			} else if (targetAppName == 'baidu') {
				url = `baidumap://map/direction?destination=name:${name}|latlng:${lat},${lon}&mode=driving&coord_type=gcj02&src=thirdapp.navi.mybaoxiu.wxy#Intent;scheme=bdapp;package=com.baidu.BaiduMap;end`;
				webUrl = webUrlBaidu;
			}
		} else if (Platform.OS == 'ios') {//ios

			if (targetAppName == 'gaode') {
				url = `iosamap://path?sourceApplication=appname&dlon=${lon}&dlat=${lat}&dname=${name}&dev=0&m=0&t=0`;
				webUrl = webUrlGaode;
			} else if (targetAppName == 'baidu') {
				url = `baidumap://map/direction?destination=name:${name}|latlng:${lat},${lon}&mode=driving&coord_type=gcj02&src=thirdapp.navi.mybaoxiu.wxy#Intent;scheme=bdapp;package=com.baidu.BaiduMap;end`;
				webUrl = webUrlBaidu;
			}

		}

		Linking.canOpenURL(url).then(supported => {
			if (!supported) {
				console.log('Can\'t handle url: ' + url);
				return Linking.openURL(webUrl).catch(e => console.warn(e));
			} else {
				return Linking.openURL(url).catch(e => console.warn(e));
			}
		}).catch(err => console.error('An error occurred', err));
	},


};
module.exports = MyUtil;